<template>
  <div class="page">
    <div class="tools-bar">
      <div class="left-wrap">
        <el-input prefix-icon="el-icon-search"  placeholder="请输入搜索关键字" size="small" v-model="searchKey"></el-input>
        <el-button size="small" type="primary" @click="handleSearch">查询</el-button>
        <el-button size="small" type="warning" @click="cleanSearch">重置</el-button>
        <!-- {{moduleId(type)}} -->
        <el-button type="primary" size="small" @click="handleAddApp">新增</el-button>
      </div>
    </div>
    <!--<el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="orderId"
        label="订单号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="appName"
        label="绑定的APP"
        width="180">
      </el-table-column>
      <el-table-column
        prop="isPay"
        label="是否支付"
        width="120">
        <template slot-scope="scope">
          <el-tag size="small" type="sucess" v-if="scope.row.isPay == 0">
            已支付
          </el-tag>
          <el-tag size="small" type="info" v-if="scope.row.isPay == 1">
            未支付
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="startDate"
        label="生效时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="loseDate"
        label="失效时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="month"
        label="购买时长（月）"
        width="120">
      </el-table-column>
      <el-table-column
        prop="region"
        label="地区"
        width="180">
      </el-table-column>
      <el-table-column
        prop="totalPrice"
        label="总价（元）"
        width="180">
      </el-table-column>
      <el-table-column
        prop="speedId"
        label="是否加速"
        width="120">
        <template slot-scope="scope">
          <el-tag size="small" type="sucess" v-if="scope.row.speedId == 2">
            加速
          </el-tag>
          <el-tag size="small" type="info" v-if="scope.row.speedId == 3">
            未加速
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="activeId"
        label="日活"
        width="100">
        <template slot-scope="scope">
          <el-tag size="small" type="sucess" v-if="scope.row.activeId == 0">
            1000
          </el-tag>
          <el-tag size="small" type="sucess" v-if="scope.row.activeId == 1">
            2000
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-edit" size="small" @click="handleEdit(scope.row)" title="编辑"></el-button>
          <el-button type="success" icon="el-icon-setting" size="small" @click="editOrderStatus(scope.row.id)" title="设为已支付"></el-button>
        </template>
      </el-table-column>
    </el-table>-->
    <el-table
      :data="tableData4"
      max-height="500"
      style="width: 100%">
      <el-table-column
        prop="orderId"
        label="订单号"
        align="center"
        width="263">
      </el-table-column>
      <el-table-column
        prop="appName"
        label="绑定的APP"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="isPay"
        label="是否支付"
        align="center"
        width="80">
        <template slot-scope="scope">
          <el-tag size="small" type="sucess" v-if="scope.row.isPay == 0">
            已支付
          </el-tag>
          <el-tag size="small" type="info" v-if="scope.row.isPay == 1">
            未支付
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderStatus"
        label="订单状态"
        align="center"
        width="80">
        <template slot-scope="scope">
          <el-tag size="small" type="sucess" v-if="scope.row.orderStatus == 0">
            待付款
          </el-tag>
          <el-tag size="small" type="info" v-if="scope.row.orderStatus == 1">
            待生效
          </el-tag>
          <el-tag size="small" type="sucess" v-if="scope.row.orderStatus == 2">
            生效中
          </el-tag>
          <el-tag size="small" type="info" v-if="scope.row.orderStatus == 3">
            已失效
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="startDate"
        label="生效时间"
        align="center"
        width="110">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.startDate | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="loseDate"
        label="生效时间"
        align="center"
        width="130">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.loseDate | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="month"
        label="购买时长(月)"
        align="center"
        width="105">
      </el-table-column>
      <el-table-column
        prop="region"
        label="地区"
        align="center"
        width="70">
      </el-table-column>
      <el-table-column
        prop="totalPrice"
        label="总价(元)"
        align="center"
        width="100">
      </el-table-column>
      <el-table-column
        prop="speedId"
        label="是否加速"
        align="center"
        width="83">
        <template slot-scope="scope">
          <el-tag size="small" type="sucess" v-if="scope.row.speedId == 2">
            加速
          </el-tag>
          <el-tag size="small" type="info" v-if="scope.row.speedId == 3">
            未加速
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="activeId"
        label="日活"
        align="center"
        width="80">
        <template slot-scope="scope">
          <el-tag size="small" type="sucess" v-if="scope.row.activeId == 0">
            1000
          </el-tag>
          <el-tag size="small" type="sucess" v-if="scope.row.activeId == 1">
            2000
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isPay == 1" type="text" icon="el-icon-edit" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button  v-if="scope.row.isPay == 1" type="text" icon="el-icon-setting" size="small" @click="editOrderStatus(scope.row.id)">支付</el-button>
          <el-button  v-if="scope.row.orderStatus == 1" type="text" icon="el-icon-setting" size="small" @click="pendingOrderStatus(scope.row.id)">待生效</el-button>
          <el-button  v-if="scope.row.orderStatus == 2" type="text" icon="el-icon-setting" size="small" @click="renewalOrderStatus(scope.row)">续费</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination ref="pageInfo" :total="pageInfo.totalElements" :page.sync="pageInfo.currentPage" @pagination="pagination"></pagination>
    <el-dialog title="订单新增或编辑" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="选择应用" prop="applicationId">
          <template>
            <el-select v-model="ruleForm.applicationId" placeholder="请选择">
              <el-option
                v-for="item in apps"
                :key="item.id"
                :label="item.appName"
                :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="购买时长（月）" prop="month">
          <el-input-number v-model="ruleForm.month"  :min="1" :max="36" @change="monthChange"></el-input-number>
        </el-form-item>
        <el-form-item label="是否加速" prop="isSpeed">
          <el-switch
            v-model="isSpeed"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="加速"
            inactive-text="不加速" @change="isSpeedChange">
          </el-switch>
        </el-form-item>
        <el-form-item label="地区" prop="region"  :class="classF">
          <template>
            <el-select v-model="ruleForm.region" placeholder="请选择" filterable>
              <el-option
                v-for="item in regions"
                :key="item.countryId"
                :label="item.chineseName"
                :value="item.chineseName">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="日活" prop="activeId">
          <template>
            <el-select v-model="ruleForm.activeId" placeholder="请选择" filterable @change="activeIdChange">
              <el-option
                v-for="item in actives"
                :key="item.configCode"
                :label="item.configItem"
                :value="item.configCode">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="总价(元)" prop="totalPrice">
          <el-input v-model="ruleForm.totalPrice" :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
   <div>
    <el-dialog title="订单续费" :visible.sync="dialogFormVisible2" >
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="应用名称" prop="appName">
          <el-input v-model="ruleForm.appName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="购买时长(月)" prop="month">
          <el-input-number v-model="ruleForm.month"  :min="1" :max="36" @change="monthChange"></el-input-number>
        </el-form-item>
       <el-form-item label="是否加速" prop="isSpeed">
          <el-switch
            v-model="isSpeed"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="加速"
            inactive-text="不加速" @change="isSpeedChange">
          </el-switch>
        </el-form-item>
        <el-form-item label="地区" prop="region"  :class="classF">
          <template>
            <el-select v-model="ruleForm.region" placeholder="请选择" filterable>
              <el-option
                v-for="item in regions"
                :key="item.countryId"
                :label="item.chineseName"
                :value="item.chineseName">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="日活" prop="activeId">
          <template>
            <el-select v-model="ruleForm.activeId" placeholder="请选择" filterable @change="activeIdChange">
              <el-option
                v-for="item in actives"
                :key="item.configCode"
                :label="item.configItem"
                :value="item.configCode">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="总价(元)" prop="totalPrice">
          <el-input v-model="ruleForm.totalPrice" :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="submit2">确 定</el-button>
      </div>
    </el-dialog>
    </div>
  </div>
</template>
<script>
  import { api } from '@/utils'
  import { pagination } from '@/components'
  import { formatDate } from '../../utils/formatDate'
  export default {
    filters: {
      formatDate (data) {
        let temp = new Date(data)
        return formatDate(temp,'yyyy-MM-dd')
      }
    },
    methods: {
      pendingOrderStatus (data) {
        let target = this
        this.$axios.request({
          url: api.pendingOrderStatus,
          method: 'post',
          params: {
            id: data
          }
        }).then(data => {
          if (data.success === true) {
            this.$message({
              message: '订单生效成功',
              duration: 2000,
              type: 'success',
              center: true
            })
            target.getData()
          } else {
            this.$message({
              message: '订单生效失败',
              duration: 2000,
              type: 'error',
              center: true
            })
          }
        })
      },
      editOrderStatus (data) {
        let target = this
        this.$axios.request({
          url: api.updateOrderStatus,
          method: 'post',
          params: {
            id: data
          }
        }).then(data => {
          if (data.success === true) {
            this.$message({
              message: '订单支付成功',
              duration: 2000,
              type: 'success',
              center: true
            })
            target.getData()
          } else {
            this.$message({
              message: '订单支付失败',
              duration: 2000,
              type: 'error',
              center: true
            })
          }
        })
      },

      countPrice () {
        let speefPrices = 0

        let activePrices = 0
        for (var i = 0; i < this.speeds.length; i++) {
          if(this.isSpeed === true){
            if (this.speeds[i]['configItem'] === '加速') {
              speefPrices = this.speeds[i]['configPrice']
              this.ruleForm.speedId = this.speeds[i]['configCode']
              break
            }
          }else {
            if (this.speeds[i]['configItem'] === '不加速') {
              speefPrices = this.speeds[i]['configPrice']
              this.ruleForm.speedId = this.speeds[i]['configCode']
              break
            }
          }
        }
        for (var j = 0; j < this.actives.length; j++) {
          if (this.actives[j]['configCode'] === this.ruleForm.activeId) {
            activePrices = this.actives[j]['configPrice']
          }
        }
        this.ruleForm.totalPrice = (speefPrices + activePrices) * this.ruleForm.month
      },
      monthChange () {
        this.countPrice()
      },
      isSpeedChange () {
        this.countPrice()
          if (this.isSpeed === true) {
            this.classF = 'class-play'
          } else {
            this.classF = 'class-hide'
          }
      },
      activeIdChange () {
        this.countPrice()
      },
      cleanForm (data) {
        Object.keys(data).forEach(function (key) {
          data[key] = ''
        })
      },
      handleAddApp () {
        this.dialogFormVisible = true
        // 清空form中的数据
        this.cleanForm(this.ruleForm)
      },
      handleEdit (data) {
        this.ruleForm = data
        this.dialogFormVisible = true
      },
      renewalOrderStatus (data) {
        this.ruleForm = data
        this.dialogFormVisible2 = true
      },
      submit () {
        let url = ''
        if (this.ruleForm.id !== '') {
          url = api.editOrder
        } else {
          url = api.addOrder
        }
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            delete this.ruleForm['updateDate']
            delete this.ruleForm['createDate']
            delete this.ruleForm['startDate']
            delete this.ruleForm['loseDate']
            this.$axios.request({
              url: url,
              method: 'post',
              params: this.ruleForm,
            }).then(data => {
              if (data.success === true) {
                this.$message({
                  message: '添加订单成功',
                  duration: 2000,
                  type: 'success',
                  center: true
                })
                this.dialogFormVisible = false
                this.getData()
              } else {
                this.$message({
                  message: '添加订单失败',
                  duration: 2000,
                  type: 'error',
                  center: true
                })
              }
            })
          } else {
            return false
          }
        })
      },
      submit2 () {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            delete this.ruleForm['updateDate']
            delete this.ruleForm['createDate']
            delete this.ruleForm['startDate']
            delete this.ruleForm['loseDate']
            this.$axios.request({
              url: api.renewalMdfTbOrder,
              method: 'post',
              params: this.ruleForm
            }).then(data => {
              if (data.success === true) {
                this.$message({
                  message: '订单续费成功',
                  duration: 2000,
                  type: 'success',
                  center: true
                })
                this.dialogFormVisible2 = false
                this.getData()
              } else {
                this.$message({
                  message: '订单续费失败',
                  duration: 2000,
                  type: 'error',
                  center: true
                })
              }
            })
          } else {
            return false
          }
        })
      },
      handleSearch () {
        let params = {}
        params.appName = this.searchKey
        this.getData(params)
      },
      cleanSearch () {
        this.searchKey = ''
      },
      // 获取数据
      getData (param) {
        console.log(param)
        this.$axios.request({
          url: api.orderList,
          method: 'post',
          params: param
        }).then(data => {
          let result = data.data
          this.pageInfo.totalElements = data.total
          if (data.success === true) {
            this.tableData4 = result
          } else {
            this.$message({
              message: '请求失败',
              duration: 2000,
              type: 'error',
              center: true
            })
          }
        })
      },
      // 列表分页操作事件
      pagination (e) {
        let params = {}
        params.pageNum = e.page
        params.pageSize = e.limit
        this.getData(params)
      },
      getSelecData () {
        this.$axios.request({
          url: api.getSpeeds,
          method: 'post'
        }).then(data => {
          let result = data.data
          if (data.success === true) {
            this.speeds = result
          } else {
            this.$message({
              message: '请求失败',
              duration: 2000,
              type: 'error',
              center: true
            })
          }
        })

        this.$axios.request({
          url: api.getAppForSelec,
          method: 'post'
        }).then(data => {
          let result = data.data
          if (data.success === true) {
              this.apps = result
            } else {
              this.$message({
                message: '请求失败',
                duration: 2000,
                type: 'error',
                center: true
              })
           }
        })

        this.$axios.request({
          url: api.getActives,
          method: 'post'
        }).then(data => {
          let result = data.data
          if (data.success === true) {
            this.actives = result
          } else {
            this.$message({
              message: '请求失败',
              duration: 2000,
              type: 'error',
              center: true
            })
          }
        })

        this.$axios.request({
          url: api.getRegionsData,
          method: 'post'
        }).then(data => {
          let result = data.data
          if (data.success === true) {
            this.regions = result
          } else {
            this.$message({
              message: '请求失败',
              duration: 2000,
              type: 'error',
              center: true
            })
          }
        })
      }
    },

    data () {
      return {
        classF: 'class-hide',
        isSpeed: false,
        actives: [],
        speeds: [],
        apps: [],
        regions: [],
        tableData4: [],
        pageInfo: {
          totalElements: 0,
          currentPage: 1
        },
        searchKey: '',
        ruleForm: {
          id: '',
          month: 1,
          applicationId: '',
          appName: '',
          regionId: '',
          region: '',
          totalPrice: '',
          speedId: '',
          activeId: 0
        },

        rules: {
          appName: [
            { required: true, message: '请输入应用名称', trigger: 'blur' }
          ]
        },
        dialogFormVisible: false,
        dialogFormVisible2: false
      }
    },
    components: {
      pagination
    },
    mounted () {
      this.getData()
      this.getSelecData()
      this.isSpeedChange()
    }
  }
</script>
<style lang="scss" scoped>
  .class-hide {
    display: none;
  }
  .class-play {
    display: inline-flex;
  }
</style>
